package com.gxpt.app.view;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gxpt.app.BaseActivity;
import com.gxpt.app.Constant;
import com.gxpt.app.MessageEvent;
import com.gxpt.app.R;
import com.utils.T;

import org.greenrobot.eventbus.EventBus;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

/**
 * Created by dufeng on 2017/8/4.
 */

@ContentView(R.layout.activity_invoice_info)
public class Activity_invoice_info extends BaseActivity {

    @ViewInject(R.id.toback)
    Button toback;

    @ViewInject(R.id.txt_title)
    TextView txt_title;

    @ViewInject(R.id.isChecked)
    CheckBox isChecked;

    @ViewInject(R.id.toedit)
    RelativeLayout toedit;

    @ViewInject(R.id.toOK)
    Button toOK;



    @ViewInject(R.id.rp_type)
    RadioGroup rp_type;

    @ViewInject(R.id.invoice_typeT1)
    RadioButton invoice_typeT1;

    @ViewInject(R.id.invoice_typeT2)
    RadioButton invoice_typeT2;


    @ViewInject(R.id.rp_info_type)
    RadioGroup rp_info_type;

    @ViewInject(R.id.radioButton11)
    RadioButton radioButton11;

    @ViewInject(R.id.radioButton22)
    RadioButton radioButton22;

    @ViewInject(R.id.radioButton33)
    RadioButton radioButton33;

    @ViewInject(R.id.radioButton44)
    RadioButton radioButton44;

    @ViewInject(R.id.editText1)
    EditText editText1;

    @ViewInject(R.id.editText2)
    EditText editText2;

    @ViewInject(R.id.editText3)
    EditText editText3;


    Bundle d = null;

    int invoice_type = 0 ;//发票类型 0.个人 1.企业
    int invoice_desc = 0 ;// 0明细 1耗材 2办公用品3 电脑配件

    String invoice_title;
    String invoice_phone;
    String invoice_email;
    boolean isCD;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);

        d            = getIntent().getBundleExtra("DATA");
        if(d!=null){
            invoice_type = d.getInt("invoice_type");
            invoice_desc = d.getInt("invoice_desc");
            invoice_title = d.getString("invoice_title");
            invoice_phone = d.getString("invoice_phone");
            invoice_email = d.getString("invoice_email");
        }
        initView();
    }

    @Override
    public void initView() {
        txt_title.setText(getResources().getString(R.string.txt_fapiaoinfo));

        if(invoice_type == 0){
            invoice_typeT1.setChecked(true);
            editText1.setVisibility(View.GONE);
        }else{
            invoice_typeT2.setChecked(true);
            editText1.setVisibility(View.VISIBLE);
        }
        rp_type.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId == R.id.invoice_typeT1){
                    invoice_type = 0 ;
                    editText1.setVisibility(View.GONE);
                }else{
                    invoice_type = 1 ;
                    editText1.setVisibility(View.VISIBLE);
                }
            }
        });


        if(invoice_desc == 0){
            radioButton11.setChecked(true);
        }else if(invoice_desc == 1){
            radioButton22.setChecked(true);
        }else if(invoice_desc == 2){
            radioButton33.setChecked(true);
        }else {
            radioButton44.setChecked(true);
        }

        rp_info_type.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId == R.id.radioButton11){
                    invoice_desc = 0 ;
                }else if(checkedId == R.id.radioButton22){
                    invoice_desc = 1 ;
                }else if(checkedId == R.id.radioButton33){
                    invoice_desc = 2 ;
                }else {
                    invoice_desc = 3 ;
                }
            }
        });

        editText1.setText(invoice_title);
        editText2.setText(invoice_phone);
        editText3.setText(invoice_email);

    }

    @Override
    public void initData() {

    }


    @Event(value={R.id.toback,R.id.toOK})
    private void getEvent(View view){
        switch(view.getId()){
            case R.id.toback:
                closeActivity();
                break;
            case R.id.toOK:
                if(checkPhone()){
                    EventBus.getDefault().post(new MessageEvent(Constant.ACTION_invoice_info,
                            invoice_type,
                            invoice_desc,
                            1,
                            editText1.getText().toString(),
                            editText2.getText().toString(),
                            editText3.getText().toString()
                    ));
                    closeActivity();
                }

                break;
        }
    }


    public boolean checkPhone() {
        if (invoice_type == 1) {
            if (editText1.getText().toString().equals("")) {
                T.show(context, getResources().getString(R.string.alert_putisnotnull), 1);
                return false;
            } else {
                if (editText2.getText().toString().equals("")) {
                    T.show(context, getResources().getString(R.string.alert_putisnotnull), 1);
                    return false;
                } else if (editText2.getText().toString().length() < 11) {
                    T.show(context, getResources().getString(R.string.alert_putPhone_Good), 1);
                    return false;
                } else {
                    return true;
                }
            }
        }else{
            if (editText2.getText().toString().equals("")) {
                T.show(context, getResources().getString(R.string.alert_putisnotnull), 1);
                return false;
            } else if (editText2.getText().toString().length() < 11) {
                T.show(context, getResources().getString(R.string.alert_putPhone_Good), 1);
                return false;
            } else {
                return true;
            }
        }
    }
}
